﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnowledgeCommunity.Web.DbHelper;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.Controllers
{
    public class ShopController : Controller
    {
        ShopHelper myShopHelper = new ShopHelper();
        CommodityHelper myCommodityHelper = new CommodityHelper();
        //
        // GET: /Shop/

        public ActionResult Index()
        {
            var Shop = myShopHelper.FindAll();
            ViewData["ShopList"] = Shop;
            return View();
        }

        [Authorize()]
        public ActionResult List()
        {
            var Shop = myShopHelper.FindAll();
            ViewData["ShopList"] = Shop;
            return View();

        }

        public ActionResult Details(int id)
        {
            var model = myShopHelper.FindById(id);
            var com = myCommodityHelper.FindByShapeCode(model.ShapeCode);
            ViewData["Detail"] = com;
            return View(model);
        }

        [Authorize]
        public ActionResult Buy(int id) 
        {
            var model = myCommodityHelper.FindById(id);
            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create()
        {
            string _id = Request.Form["Id"];
            string code = Request.Form["ShapeCode"];
            string count = Request.Form["buyCount"];
            int Id = 0;
            var model = new Shop();
            if (_id != "" && code != "")
            {
                Id = Convert.ToInt32(_id);
                model.Count = Convert.ToInt32(count);
                model.UserName = HttpContext.User.Identity.Name;
                model.AddTime = DateTime.Now;
                model.ShapeCode = code;
                model.C_id = Id;
                model.State = 0;
                myShopHelper.Create(model);
                return RedirectToAction("MyOrder", "Zone");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            var model = myShopHelper.FindById(id);
            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            bool n = myShopHelper.Delete(id);
            if (n)
            {
                return RedirectToAction("List", "Shop");
            }
            else
                return RedirectToAction("Error", "Notice", new { type = 2 });
        }

        [Authorize()]
        public ActionResult Approve(int id)
        {
            var model = myShopHelper.FindById(id);
            model.State = 1;
            DoUpdateModel(model);
            return RedirectToAction("List", "Shope");
        }

        [Authorize()]
        public ActionResult Cancel(int id)
        {
            var model = myShopHelper.FindById(id);
            model.State = 2;
            DoUpdateModel(model);
            return RedirectToAction("List", "Shope");
        }

        CKCDB mydb = new CKCDB();

        private void DoUpdateModel(Shop model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Commodities.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Shop>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }


    }
}
